package isi_ib;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Properties;

import com.ib.client.Contract;

public class MyContract {

	private Properties prop ;
	public Contract contrato;
	private String fichero;
	
	//ccontrato.contrato.m_symbol = tm_symbol.getText();
	//ccontrato.contrato.m_secType= cbsecType.getSelectedItem().toString();
	//ccontrato.contrato.m_expiry=tm_expiry.getText();
	//ccontrato.contrato.m_exchange=tm_exchange.getText();
	//ccontrato.contrato.m_currency=tm_currency.getText();
	
	
	
	public String getFichero() {
		if(fichero.isEmpty()) return "config.properties";
		else
		return fichero;
	}

	
	public void setM_includeExpired(boolean m_includeExpired) {
		this.contrato.m_includeExpired = m_includeExpired;
	}
	
	public void setM_currency(String m_currency) {
		this.contrato.m_currency = m_currency;
	}
	public void setM_exchange(String m_exchange) {
		this.contrato.m_exchange = m_exchange;
	}
	public void setM_expiry(String m_expiry) {
		//YYYYMM
		this.contrato.m_expiry = m_expiry;
	}
	public void setM_symbol(String m_symbol) {
		this.contrato.m_symbol = m_symbol;
	}
	public void setM_secType(String m_secType) {
		//STK,OPT,FUT,IND,FOP,CASH,BAG
		this.contrato.m_secType = m_secType;
	}

	public MyContract(String fichero_,String symbol_){
		prop = new Properties();
		contrato = new Contract();
		if(!symbol_.isEmpty()) {
			contrato.m_symbol=symbol_;
			try {
				Carga(symbol_);
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		fichero=fichero_;
		
	}
	
public void Carga(String symbol_) throws Exception {
	
	if((new File(symbol_+"."+getFichero())).isFile())
	{
	FileInputStream f_=new FileInputStream(symbol_+"."+getFichero());
	prop.load(f_);
	f_.close();	
		
	contrato.m_symbol=prop.getProperty("m_symbol","");
	contrato.m_currency=prop.getProperty("m_currency","");
	contrato.m_exchange=prop.getProperty("m_exchange","");
	contrato.m_expiry=prop.getProperty("m_expiry","");
	//contrato.m_localSymbol=prop.getProperty("m_localSymbol","");
	//contrato.m_multiplier=prop.getProperty("m_multiplier","");
	//contrato.m_primaryExch=prop.getProperty("m_primaryExch","");
	//contrato.m_right=prop.getProperty("m_right","");
	//	contrato.m_secId=prop.getProperty("m_secId","");
//	contrato.m_secIdType=prop.getProperty("m_secIdType","");
	contrato.m_secType=prop.getProperty("m_secType","");
	contrato.m_strike=Integer.parseInt(prop.getProperty("m_strike","0"));
	contrato.m_includeExpired=(prop.getProperty("m_includeExpired","1")=="1");
	
	//contrato.m_right=prop.getProperty("m_right","");
	}
	
					//
		
	}
	
	public void Guarda() {
		
		if((contrato.m_symbol==null)) return;
		if(!(contrato.m_symbol==null)) prop.setProperty("m_symbol", contrato.m_symbol);//This is the symbol of the underlying asset.
		//if(!(contrato.m_comboLegsDescrip==null)) prop.setProperty("m_comboLegsDescrip",contrato.m_comboLegsDescrip);
		if(!(contrato.m_currency==null))prop.setProperty("m_currency", contrato.m_currency);
		if(!(contrato.m_exchange==null))prop.setProperty("m_exchange", contrato.m_exchange);
		if(!(contrato.m_expiry==null))prop.setProperty("m_expiry", contrato.m_expiry); //Use the format YYYYMM.
		if(!(contrato.m_localSymbol==null))prop.setProperty("m_localSymbol", contrato.m_localSymbol);
		if(!(contrato.m_multiplier==null))prop.setProperty("m_multiplier", contrato.m_multiplier);
		if(!(contrato.m_primaryExch==null))prop.setProperty("m_primaryExch", contrato.m_primaryExch);
		if(!(contrato.m_right==null))prop.setProperty("m_right", contrato.m_right);//Specifies a Put or Call. Valid values are: P, PUT, C, CALL.
		if(!(contrato.m_secId==null))prop.setProperty("m_secId", contrato.m_secId);
		if(!(contrato.m_secIdType==null))prop.setProperty("m_secIdType", contrato.m_secIdType);
		if(!(contrato.m_secType==null))prop.setProperty("m_secType", contrato.m_secType); // STK,OPT,FUT,IND,FOP,,CASH,BAG
		prop.setProperty("m_includeExpired", (contrato.m_includeExpired?"1":"0")); // STK,OPT,FUT,IND,FOP,,CASH,BAG
		
		//prop.setProperty("m_conId", String.valueOf(contrato.m_conId));
		//if(!(contrato.m_comboLegs==null))prop.setProperty("m_comboLegs", contrato.m_comboLegs.toString());
		//prop.setProperty("m_includeExpired", String.valueOf(contrato.m_includeExpired));
		if(!(contrato.m_comboLegs==null))prop.setProperty("m_comboLegs", String.valueOf(contrato.m_strike));
		try {
		FileOutputStream f_=new FileOutputStream(contrato.m_symbol+"."+getFichero());
		prop.store(f_, null);
		f_.close();
		}catch (IOException ex) {ex.printStackTrace();}
	}
	public String toString(){
		
		return(
				contrato.toString()+
				"\n m_symbol->"+contrato.m_symbol+
				"\n m_currency->"+contrato.m_currency+
				"\n m_exchange->"+contrato.m_exchange+
				"\n m_expiry->"+contrato.m_expiry+
				"\n m_localSymbol->"+contrato.m_localSymbol+
				"\n m_multiplier->"+contrato.m_multiplier+
				"\n m_primaryExch->"+contrato.m_primaryExch+
				"\n m_right->"+contrato.m_right+
				"\n m_secId->"+contrato.m_secId+
				"\n m_secIdType->"+contrato.m_secIdType+
				"\n m_secType->"+contrato.m_secType+
				"\n m_strike->"+contrato.m_strike+
				"\n m_right->"+contrato.m_right	+
				"\n m_includeExpired->"+(contrato.m_includeExpired?"true":"false")
				
				);
	}
	
}
